import Vue from 'vue'
import Router from 'vue-router'

const routes = [{
		path: '/', //根目录
		redirect: '/home' //跳转到home路径
	},
	{
		path: '/login',
		component: () => import('./components/login'),
		meta: {
			requiresAuth: false
		}
	},
	{
		path: '/home',
		component: () => import('./components/Home'), //方法的方式是懒加载
		meta: {
			requiresAuth: false
		}
	},
	{
		path: '/about',
		component: () => import('./components/About')
	},
	{
		path: '/user',
		name: 'user',
		component: () => import('./components/user/user_list'),
	},
	{
		path: '/user/detail/:id',
		name: 'user_detail',
		component: () => import('./components/user/user_detail')
	},
	{
		path: '/news',
		name: 'news',
		component: () => import('./components/news/news_list'),
		children: [{
			path: 'detail/:id',
			name: 'news_detail',
			component: () => import('./components/news/news_detail')
		}]
	},
	{
		path: '*', //需要放在最后，没有匹配其他路由则走这里
		component: () => import('./components/404')
	},
];

Vue.use(Router)

const router = new Router({
	routes,
	mode: 'history' //默认hash模式,history模式需要后台改配置
})

//导航守卫，权限校验
router.beforeEach((to, from, next) => {
	if (to.meta.requiresAuth != undefined && !to.meta.requiresAuth) {
		//不需要权限校验，直接放行
		next();
		return;
	}
	next('/login');
})


export default router
